package com.ly.emp.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ly.common.core.bean.R;
import com.ly.common.core.bean.dto.CommonBatchUpdateDTO;
import com.ly.emp.bean.dto.HubDTO;
import com.ly.emp.entity.HubTeamQuota;
import com.ly.emp.entity.HubTeamQuotaOrg;
import com.ly.emp.mapper.EpmBaseSuitunitOrgMapper;
import com.ly.emp.mapper.HubTeamQuotaOrgMapper;
import com.ly.emp.service.IHubTeamQuotaOrgService;
import com.ly.emp.service.IHubTeamQuotaService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 基础定额配置-org-impl
 *
 * @author luoyun
 * @Date: 2025-05-16
 */
@Service
@RequiredArgsConstructor
public class HubTeamQuotaOrgServiceImpl extends ServiceImpl<HubTeamQuotaOrgMapper, HubTeamQuotaOrg> implements IHubTeamQuotaOrgService {

    private final IHubTeamQuotaService quotaService;
    private final EpmBaseSuitunitOrgMapper suitunitOrgMapper;

    @Override
    public R getId(String id) {
        HubTeamQuotaOrg entity = this.getById(id);
        if (entity == null) {
            return R.fail("未找到对应数据");
        }
        return R.ok(entity);
    }

    @Override
    public R getList(HubTeamQuotaOrg entity) {
        List<HubTeamQuotaOrg> list = this.list(
                new LambdaQueryWrapper<HubTeamQuotaOrg>()
                        .eq(StrUtil.isNotEmpty(entity.getOrgCode()), HubTeamQuotaOrg::getOrgCode, entity.getOrgCode())
        );
        return R.ok(list);
    }

    @Override
    public R getPage(HubTeamQuotaOrg entity, Page<HubTeamQuotaOrg> page) {
        IPage<HubTeamQuotaOrg> pageList = this.page(
                page,
                new LambdaQueryWrapper<HubTeamQuotaOrg>()
                        .eq(StrUtil.isNotEmpty(entity.getOrgCode()), HubTeamQuotaOrg::getOrgCode, entity.getOrgCode())
                        .orderByAsc(HubTeamQuotaOrg::getOrgCode)
                        .orderByAsc(HubTeamQuotaOrg::getDisplayOrder)
        );
        return R.ok(pageList);
    }

    @Override
    public R add(HubTeamQuotaOrg entity) {
        boolean flag = this.save(entity);
        return flag ? R.ok(true, "创建成功") : R.fail("创建失败");
    }

    @Override
    public R edit(HubTeamQuotaOrg entity) {
        boolean flag = this.updateById(entity);
        return flag ? R.ok(true, "修改成功") : R.fail("修改失败");
    }

    @Override
    public R delete(String id) {
        boolean flag = this.removeById(id);
        return flag ? R.ok(true, "删除成功") : R.fail("删除失败");
    }

    @Override
    public R deleteBatch(String ids) {
        boolean flag = this.removeByIds(Arrays.asList(ids.split(",")));
        return flag ? R.ok(true, "批量删除成功") : R.fail("批量删除失败");
    }

    @Override
    public R updateBatch(CommonBatchUpdateDTO<HubTeamQuotaOrg> dto) {
        List<HubTeamQuotaOrg> datas = dto.getDatas();
        boolean flag = this.saveOrUpdateBatch(datas);
        return flag ? R.ok(true, "批量修改成功") : R.fail("批量修改失败");
    }

    @Override
    public R init(HubDTO dto) {
        // 原始数据
        R r = quotaService.getList(new HubTeamQuota());
        List<HubTeamQuota> quotaList = (List<HubTeamQuota>) r.getResult();
        // 当前已存在的
        List<HubTeamQuotaOrg> existList = this.list(
                new LambdaQueryWrapper<HubTeamQuotaOrg>()
                        .eq(HubTeamQuotaOrg::getOrgCode, dto.getOrgCode())
                        .orderByAsc(HubTeamQuotaOrg::getDisplayOrder)
        );
        List<String> existCodeList = existList.stream().map(n -> n.getFactorCode()).collect(Collectors.toList());
        // 保存不存在的
        String orgCode = dto.getOrgCode();
        String orgName = suitunitOrgMapper.getNameByCode(orgCode);
        List<HubTeamQuotaOrg> noList = new ArrayList<>();
        quotaList.stream()
                .filter(n -> !existCodeList.contains(n.getFactorCode()))
                .forEach(n -> {
                    HubTeamQuotaOrg entity = new HubTeamQuotaOrg();
                    entity.setOrgCode(orgCode);
                    entity.setOrgName(orgName);
                    entity.setFactorCode(n.getFactorCode());
                    entity.setFactorName(n.getFactorName());
                    noList.add(entity);
                });
        boolean b = this.saveOrUpdateBatch(noList);
        String msg = "初始化成功，同步了" + noList.size() + "条数据";
        return R.ok(true, msg);
    }
}
